package br.edu.catolica.sadbc.core.manager;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Scope;

import br.edu.catolica.sadbc.core.entity.AbstractEntity;
import br.edu.catolica.sadbc.core.exception.BusinessObjectException;

/**
 * 
 * @author robert.resende
 *
 * @param <T>
 */
@Scope(ScopeType.CONVERSATION)
public abstract class AbstractCrudManager<T extends AbstractEntity> extends AbstractManager {

	public String salvar(T entity) throws BusinessObjectException {
		salvar(entity, true);
		return RESULT_INSERT;
	}

	public String update(T entity) throws BusinessObjectException {
		salvar(entity, true, true);
		return RESULT_UPDATE;
	}

//	public String excluir(T entity) throws BusinessObjectException {
//		salvar(entity, true);
//		return RESULT_EXCLUDE;
//	}
	
	public String excluir(T entity) throws BusinessObjectException {
		excluir(entity, true);
		return RESULT_EXCLUDE;
	}

	protected void salvar(T entity, boolean flushed) throws BusinessObjectException {
		if (entity.getId() != null) {
			salvar(entity, true, flushed);
		} else {
			salvar(entity, false, flushed);
		}
	}

}
